<?php
/**
 * fly
 * Copyright By 5920Team (http://www.5920.me)
 * User: yu@5920.me
 * Date: 13-9-27
 * Time: 下午12:53
 */


class Db
{
    /**
     * @var DbAdapter
     */
    static private $db;

    /**
     * @var
     */
    private $conf = array();


    /**
     * @param $config
     */
    public function __construct($config){
        if (self::$db === NULL)
        {
            $this->conf = $config;

            switch ($this->conf['adapter'])
            {
                case 'Mysqli':
                    self::$db = MysqliDbAdapter::run();
                    break;
                default:
                    self::$db = false;
                    break;
            }
            if(!self::$db)
            {
                exit('error Db Driver.');
            }
            self::$db->connect($this->conf['host'],
                $this->conf['username'],
                $this->conf['password'],
                $this->conf['database'],
                $this->conf['charset']);
        }
    }


    /**
     * @return SqlBuilder
     */
    public function sql(){
        return new SqlBuilder($this->conf['prefix']);
    }

    /**
     * @param $table
     * @return SqlBuilder
     */
    public function insert($table)
    {
        return $this->sql()->insert($table);
    }

    /**
     * @param $table
     * @return SqlBuilder
     */
    public function update($table)
    {
        return $this->sql()->update($table);
    }

    /**
     * @param $table
     * @return SqlBuilder
     */
    public function delete($table)
    {
        return $this->sql()->delete($table);
    }

    /**
     * @return SqlBuilder
     */
    public function select()
    {
        $args = func_get_args();
        return call_user_func_array(array($this->sql(), 'select'), $args ? $args : array('*'));
    }

    /**
     * select return array
     */
    public function fetchRow($query)
    {
        $result = self::$db->query($query);
        return self::$db->fetchAssoc($result);
    }

    /**
     * select return array
     */
    public function fetchAll($query)
    {
        $rows = array();
        $result = self::$db->query($query);
        while ($row = $result->fetch_assoc()) {
            $rows[] = $row;
        }
        return $rows;
    }
    /**
     * @param SqlBuilder $query
     * @return number
     */
    public function getCount($query)
    {
        $row = $this->fetchRow($query->setCount());
        return $row['count'];
    }

    /**
     * insert delete update
     * @param string|SqlBuilder $query
     * @return number|bool
     */
    public function query($query)
    {
        $action = $query->getAction();
        switch ($action) {
            case 'UPDATE':
                $result = self::$db->query($query);
                return self::$db->affectedRows();
            case 'DELETE':
                $result = self::$db->query($query);
                return self::$db->affectedRows();
            case 'INSERT':
                $result = self::$db->query($query);
                return self::$db->insertId();
            default:
                exit('dot use !');
        }
    }

}